{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "cc3cb02e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2  #在虚拟环境中安装，conda install opencv-python\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "from tensorflow import keras\n",
    "from tensorflow.keras import layers\n",
    "def image_read(image_path):\n",
    "    img=tf.io.read_file(image_path)\n",
    "    img=tf.io.encode_base64(img)\n",
    "    img=tf.io.decode_base64(img)\n",
    "    img=tf.io.decode_image(img)\n",
    "    img=tf.image.convert_image_dtype(img,dtype=tf.float32)\n",
    "    img=img[np.newaxis,:]\n",
    "    return img\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "47ac57bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "def layers_name():\n",
    "    pre_contents_layers=[\"block5_conv2\"]\n",
    "    pre_styles_layers=[\n",
    "        \"block1_conv1\",\n",
    "        \"block2_conv1\",\n",
    "        \"block3_conv1\",\n",
    "        \"block4_conv1\"\n",
    "    ]\n",
    "    num_style_layers=len(pre_styles_layers)\n",
    "    num_contents_layers=len(pre_contents_layers)\n",
    "    return pre_contents_layers,pre_styles_layers,num_style_layers,num_contents_layers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98574460",
   "metadata": {},
   "outputs": [],
   "source": [
    "def pre_vgg16(layers_name):\n",
    "    vgg16=tf.keras.applications.VGG16(include_top=False,weights=\"imagenet\")\n",
    "    vgg16.trainable=False\n",
    "    outputs=[vgg16.get_layer(name).output for name in layers_name]\n",
    "    model=tf.keras.Model([vgg16.input],outputs)\n",
    "    return model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c7527a65",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 1200, 1600, 3)\n",
      "tf.Tensor(\n",
      "[[[[0.13725491 0.23137257 0.09019608]\n",
      "   [0.14117648 0.23529413 0.09411766]\n",
      "   [0.14509805 0.2392157  0.09803922]\n",
      "   ...\n",
      "   [0.10980393 0.2627451  0.14117648]\n",
      "   [0.10196079 0.25490198 0.13333334]\n",
      "   [0.09411766 0.24705884 0.1254902 ]]\n",
      "\n",
      "  [[0.14901961 0.24313727 0.10196079]\n",
      "   [0.14901961 0.24313727 0.10196079]\n",
      "   [0.15294118 0.24705884 0.10588236]\n",
      "   ...\n",
      "   [0.10980393 0.2627451  0.14117648]\n",
      "   [0.10196079 0.25490198 0.13333334]\n",
      "   [0.09803922 0.2509804  0.12941177]]\n",
      "\n",
      "  [[0.16078432 0.25490198 0.1137255 ]\n",
      "   [0.16078432 0.25490198 0.1137255 ]\n",
      "   [0.16078432 0.25490198 0.1137255 ]\n",
      "   ...\n",
      "   [0.10588236 0.25882354 0.13725491]\n",
      "   [0.10588236 0.25882354 0.13725491]\n",
      "   [0.10196079 0.25490198 0.13333334]]\n",
      "\n",
      "  ...\n",
      "\n",
      "  [[0.8431373  0.6039216  0.2392157 ]\n",
      "   [0.8431373  0.60784316 0.23137257]\n",
      "   [0.83921576 0.6117647  0.23137257]\n",
      "   ...\n",
      "   [0.8000001  0.43137258 0.40000004]\n",
      "   [0.8705883  0.4901961  0.454902  ]\n",
      "   [0.9333334  0.5568628  0.5019608 ]]\n",
      "\n",
      "  [[0.8313726  0.5921569  0.227451  ]\n",
      "   [0.8352942  0.6        0.22352943]\n",
      "   [0.8313726  0.6039216  0.22352943]\n",
      "   ...\n",
      "   [0.8000001  0.43529415 0.40784317]\n",
      "   [0.86666673 0.49803925 0.4666667 ]\n",
      "   [0.93725497 0.5686275  0.52156866]]\n",
      "\n",
      "  [[0.82745105 0.5882353  0.22352943]\n",
      "   [0.82745105 0.5921569  0.21568629]\n",
      "   [0.82745105 0.6        0.21960786]\n",
      "   ...\n",
      "   [0.8078432  0.4431373  0.42352945]\n",
      "   [0.8705883  0.5019608  0.47058827]\n",
      "   [0.9450981  0.5764706  0.5372549 ]]]], shape=(1, 1200, 1600, 3), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "if __name__=='__main__':\n",
    "    image_content_path='./image/1.jpg'\n",
    "    img_contents=image_read(image_content_path)\n",
    "    print(img_content.shape)\n",
    "    print(img_contents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6eefff41",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
